<?php

namespace App\Models;

use App\Http\Controllers\Api\User\ReBateController;
use App\Http\Controllers\ApiController;
use Illuminate\Database\Eloquent\Model;

class RechargeOrder extends Model
{
    public $table = 'recharge_order';
    public static function  addRechargeOrder($input,string $order_type)
    {
       if ($order_type == 1){
           $input['order_sn'] = ApiController::get_order_sn('2');
       }else if ($order_type == '2'){
           $input['order_sn'] = ApiController::get_order_sn('3');
       }else{
           throw  new \Error('充值类型错误');
       }

       $input['order_type']     = $order_type;
       $input                   = ApiController::createData('recharge_order',$input);
       $input['created_at']     = $input['created_at'] = date('Y-m-d H:i:s');
       $flag                    = RechargeOrder::insertGetId($input);
       if ($flag>0){
           #生成分佣
           $rebate = new ReBateController();
           $rebate->RebateByUid($input['uid'],$input['order_sn'],$input['total_amount']);
       }
       return $flag;
    }


    public static function addRechargeRebateOrder($input)
    {
        $input['uid']                = $input['to_uid'];
        $input['order_type']         = 2;
        $flag                        = self::addRechargeOrder($input,$input['order_type']);
        return $flag;
    }


    public static function getRechargeOrder($order_sn)
    {
       $data = self::where('order_sn',$order_sn)->first();
       if (empty($data)){
           return [];
       }else{
           return $data->toArray();
       }
       return $data;
    }

}
